﻿/****************************************************************
 * Copyright (c) 2006-2007 Besture All Rights Reserved.
 * 
 * File Name:		Resources.cs
 * 
 * Created:		2013/06/24
 * Original Author:	Ni Tao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/
using System;
using System.Web.UI;
using System.Web;
using System.Data;
using Besture.Common.PageControl;
using Besture.Common.Definition;
using System.Web.UI.WebControls;
using System.Resources;
using System.Globalization;

namespace Besture.Common
{
    /// <summary>
    /// The base class of various pages
    /// </summary>
    public abstract class FrontBasePage : Page
    {
        private Resources resource = null;
        private CultureInfo cultureInfo = null;
        ///<summary>
        /// Page controller
        ///</summary>
        ///<remarks></remarks>
        public PageController pageController = null;

        /// <summary>
        /// Constructor
        /// </summary>
        ///<remarks></remarks>
        public FrontBasePage()
        {
            pageController = new PageController(this);
        }
        ///<summary>
        /// Page load event
        ///</summary>
        ///<remarks></remarks>
        protected override void OnLoad(System.EventArgs e)
        {
           
            if (!base.IsPostBack)
            {
                this.InitialLoad();
                this.SetErrorMessageOnLoad();
            }
            else
            {
                this.PostBackLoad();
            }
            base.OnLoad(e);
        }

        ///<summary>
        /// set error message for validation when initial loading
        ///</summary>
        ///<remarks>the method should be overrode if the page has validation</remarks>
        protected virtual void SetErrorMessageOnLoad()
        { }

        ///<summary>
        /// Initial processing
        ///</summary>
        ///<remarks>This method must be implemented.</remarks>
        protected abstract void InitialLoad();
        ///<summary>
        /// Add the specific processing when the acction is postback
        ///</summary>
        ///<remarks>This method must be implemented.</remarks>
        protected abstract void PostBackLoad();

        ///<summary>
        /// get the specific message with message ID
        ///</summary>
        ///<param name="lsID">Message Id</param>
        ///<remarks></remarks>
        protected String GetMessage(String lsID)
        {
            return MessageManager.GetMessage(lsID);
        }

        /// <summary>
        /// get the specific message with message ID and parameter
        /// </summary>
        /// <param name="lsID">message ID</param>
        /// <param name="lsInfo">message parameter</param>
        /// <returns></returns>
        protected String GetMessage(String lsID, String[] lsInfo)
        {
            return MessageManager.GetMessage(lsID, lsInfo);
        }

        ///<summary>
        /// Save the data of the current page to session  with the key
        ///</summary>
        ///<param name="strKey">key</param>
        ///<param name="obj">Parameter Object</param>
        ///<remarks></remarks>
        public void SaveToSession(string strKey, object obj)
        {
            Session[this.pageController.GetPageId(Request.Url.OriginalString) + "_" + strKey] = obj;
        }
        ///<summary>
        /// Get session Data
        ///</summary>
        ///<param name="strKey">key</param>
        ///<remarks></remarks>
        public Object GetSessionData(string strKey)
        {
            return Session[this.pageController.GetPageId(Request.Url.OriginalString) + "_" + strKey];
        }
        ///<summary>
        /// Remove the parameter of the current page from session
        ///</summary>
        ///<param name="strKey">key</param>
        ///<remarks></remarks>
        public void RemoveFromSession(string strKey)
        {
            Session.Remove(this.pageController.GetPageId(Request.Url.OriginalString) + "_" + strKey);
        }

        public delegate void EventHandlerPageChange(object sender, CommandEventArgs e);
        public event EventHandlerPageChange Click;
        protected void OnClick(CommandEventArgs e)
        {
            if (Click != null) Click(this, e);
        }

        public CultureInfo GetCurrentCulture
        {
            get
            {
                if (cultureInfo == null)
                {
                    return new CultureInfo(Session["cultureName"].ToString());
                }
                return cultureInfo;
            }
        }

        public void PrepareResource(Type typeObj)
        {
            resource = new Resources(typeObj, GetCurrentCulture);
        }

        public Resources Resource
        {
            get
            {
                return resource;
            }
        }
    }
}
